//
//  NativeUtils.m
//
//
//  Created by Paul Michael Wisdom on 5/20/15.
//
//

#import "BdNativeUtils.h"


@implementation BdNativeUtils

BOOL showPrompt;

- (void)pluginInitialize {
    showPrompt = NO;
}

- (void)getGPSState:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
       @try {
    //get command options
    //    NSLog(@"GETTING GPS STATE");
    BOOL locationEnabled = [CLLocationManager locationServicesEnabled];
    //    NSLog(@"Location services enabled? %d", locationEnabled);
    CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
    //    NSLog(@"Location Manager Auth Status %d", authStatus);
    //    NSLog(@"Denied %d Authorized %d AlwaysAuthorized %d WhenInUseAuthorized %d Restricted %d", kCLAuthorizationStatusDenied, kCLAuthorizationStatusAuthorized, kCLAuthorizationStatusAuthorizedAlways, kCLAuthorizationStatusAuthorizedWhenInUse, kCLAuthorizationStatusNotDetermined);
    
    showPrompt = [[command argumentAtIndex:0] boolValue];
    
    if([CLLocationManager locationServicesEnabled] &&
       ((authStatus == kCLAuthorizationStatusNotDetermined) ||
        (authStatus == kCLAuthorizationStatusAuthorizedAlways)  ||
        (authStatus == kCLAuthorizationStatusAuthorizedWhenInUse))) {
           
           pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @"Enabled"];
           
       } else {
           NSString *reason;
           
           if(authStatus == kCLAuthorizationStatusDenied) {
               reason = @"Denied";
       //    } else if(authStatus == kCLAuthorizationStatusNotDetermined) {
        //       reason = @"NotDetermined";
           } else if(authStatus == kCLAuthorizationStatusRestricted) {
               reason = @"Restricted";
           } else {
               reason = @"Disabled";
           }
           
           if(showPrompt) {

                  if([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0){
                       UIAlertView *tipMessage = [[UIAlertView alloc] initWithTitle:@"定位服务已关闭"
                                                                        message:@"请到设置中开启定位服务"
                                                                        delegate:self
                                                                        cancelButtonTitle:nil
                                                                        otherButtonTitles:@"知道了", nil];
                       tipMessage.tag = 1001;
                       [tipMessage show];
                   }else{
                       UIAlertView *tipSettingMessage=[[UIAlertView alloc] initWithTitle:@"定位服务已关闭" message:@"请到设置中开启定位服务" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
                       tipSettingMessage.tag=1002;
                       [tipSettingMessage show];
                   }


           }

           pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: reason];
       }
   }
    @catch (NSException *exception) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:exception.reason];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}



- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  {
    if (alertView.tag == 1002 && buttonIndex == 1){
        //code for opening settings app in iOS 8
        [[UIApplication sharedApplication] openURL:[NSURL  URLWithString:UIApplicationOpenSettingsURLString]];
    }
}

@end